package tools; 

import java.util.HashMap; 
import java.util.Map; 

import javax.servlet.http.HttpSessionAttributeListener; 
import javax.servlet.http.HttpSessionBindingEvent; 
import javax.servlet.http.HttpSessionEvent; 
import javax.servlet.http.HttpSessionListener; 

import pojo.Persion;

public class HttpSessionCounter implements HttpSessionListener, 
HttpSessionAttributeListener { 
private static int activeSessions = 0; 
private static Map<String,Persion> online=new HashMap<String,Persion>(); 
public static Map<String,Persion> getOnline() { 
return online; 
} 

public static int getActiveSessions() { 
return activeSessions; 
} 

public void attributeAdded(HttpSessionBindingEvent event) { 
if (event.getName().equals("user")) { 
Persion users = (Persion) event.getSession().getAttribute("user"); 
online.put(event.getSession().getId(),users); 

} 
} 

public synchronized void attributeRemoved(HttpSessionBindingEvent event) { 
if (event.getName().equals("user")) { 
online.remove(event.getSession().getId()); 
} 
} 

public void attributeReplaced(HttpSessionBindingEvent event) { 
} 

public void sessionCreated(HttpSessionEvent event) { 
activeSessions++; 

} 

public void sessionDestroyed(HttpSessionEvent event) { 
if (activeSessions > 0) { 
activeSessions--; 

} 
} 
} 

